package com.zq.it.cms.common;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeSet;

import org.apache.commons.lang3.StringUtils;

import com.zq.it.cms.common.util.CommUtil;

/**
 * @author zhengqiang
 * @description 后台菜单类
 */
public class Menu implements Comparable<Menu>{

	private int                id;         //ID
	private String             name;       //名称
	private String             icon;       //图标
	private String             perms;      //权限，多个用,隔开
	private String             perm;       //菜单第一个权限
	private String             url;        //路径
	private int                parentId;   //上级菜单ID
	private List<MenuOption>   ops;        //菜单对应功能模块里的具体操作，比如栏目管理里面的新增、修改、删除等功能
	private Menu               parent;     //上级
	private TreeSet<Menu>      children;   //下级列表
	
	/**
	 * 处理上级关系并排序
	 */
	public static TreeSet<Menu> sort(Collection<Menu> menus){
		Map<Integer, Menu> map = new HashMap<Integer, Menu>();
		for (Menu menu : menus) {
			map.put(menu.getId(), menu);
		}
		for (Menu menu : menus) {
			Menu parent = map.get(menu.getParentId());
			if(parent != null){
				TreeSet<Menu> children = parent.getChildren();
				if(children == null){
					children = new TreeSet<Menu>();
					parent.setChildren(children);
				}
				children.add(menu);
				menu.setParent(parent);
			}
		}
		TreeSet<Menu> result = new TreeSet<Menu>();
		for (Menu menu : menus) {
			if(menu.getParent() == null){
				result.add(menu);
			}
		}
		return result;
	}
	
	/**
	 * 将map解析为Menu对象
	 */
	public static Menu parse(Object id, Map<Object, Object> map){
		Menu menu = new Menu();
		menu.setId(CommUtil.toInt(id));
		Object name = map.get("name");
		if (name != null) {
			menu.setName(String.valueOf(name));
		}
		Object icon = map.get("icon");
		if (icon != null) {
			menu.setIcon(String.valueOf(icon));
		}
		Object perms = map.get("perms");
		if (perms != null) {
			menu.setPerms(String.valueOf(perms));
		}
		Object url = map.get("url");
		if (url != null) {
			menu.setUrl(String.valueOf(url));
		}
		Object parentId = map.get("parentId");
		if (parentId != null) {
			menu.setParentId(CommUtil.toInt(parentId));
		}
		List<Object> ops = (List<Object>) map.get("ops");
		if(ops != null){
			List<MenuOption> options = new ArrayList<Menu.MenuOption>();
			menu.setOps(options);
			for (Object op : ops) {
				MenuOption option = new MenuOption();
				String[] pair = StringUtils.split(String.valueOf(op), '@');
				option.name = pair[0];
				option.perms = null;
				if(pair.length > 1){
					option.perms = pair[1];
				}
				options.add(option);
			}
		}
		return menu;
	}
	
	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getIcon() {
		return icon;
	}

	public void setIcon(String icon) {
		this.icon = icon;
	}

	public String getPerms() {
		return perms;
	}

	public void setPerms(String perms) {
		this.perms = perms;
		if(perms != null){
			this.perm = StringUtils.split(perms, ',')[0];
		}
	}
	public String getPerm() {
		return perm;
	}
	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public int getParentId() {
		return parentId;
	}

	public void setParentId(int parentId) {
		this.parentId = parentId;
	}

	public List<MenuOption> getOps() {
		return ops;
	}

	public void setOps(List<MenuOption> ops) {
		this.ops = ops;
	}

	public Menu getParent() {
		return parent;
	}

	public void setParent(Menu parent) {
		this.parent = parent;
	}

	public TreeSet<Menu> getChildren() {
		return children;
	}

	public void setChildren(TreeSet<Menu> children) {
		this.children = children;
	}

	public static class MenuOption{
		private String name;
		private String perms;
		private String perm;
		public String getName() {
			return name;
		}
		public void setName(String name) {
			this.name = name;
		}
		public String getPerms() {
			return perms;
		}
		public void setPerms(String perms) {
			this.perms = perms;
			if(perms != null){
				this.perm = StringUtils.split(perms, ',')[0];
			}
		}
		public String getPerm() {
			return perm;
		}
	}

	@Override
	public int compareTo(Menu o) {
		return new Integer(this.id).compareTo(new Integer(o.id));
	}
}
